#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
struct Point{
	int id,x;
}wt[N<<2];
int n,x,clen[N],b[N<<2],cnt,wyt[N<<2],len;
void insert(int x){
	wyt[x]++;
	if(wyt[x]==1)cnt++;
}
void del(int x){
	wyt[x]--;
	if(!wyt[x])cnt--;
}int ans=2e9;
bool cmp(Point x,Point y){
	return x.x<y.x;
}
signed main(){
	freopen("contest.in","r",stdin);
	freopen("contest.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> clen[i];
		for(int j = 1;j <= clen[i];j++)cin >> x,wt[++len]=(Point){i,x};
	}sort(wt+1,wt+1+len,cmp);int l = 0;
	for(int i = 1;i <= len;i++){
		while(l<len&&cnt<n){
			l++;
			insert(wt[l].id);
		}
		if(l==len&&cnt<n)break;
		if(cnt==n)ans=min(ans,wt[l].x-wt[i].x);
		del(wt[i].id);
	}cout << ans;
	return 0;
}
